package com.tuling.code.thread.exam01;
/**
 * 打印偶数线程
 * @author javaw
 *
 */
public class PrintOushu implements Runnable{
	private Num num;
	
	public PrintOushu(Num num) {
		this.num = num;
	}

	@Override
	public void run() {
		while (num.i <= 100) {
			/* 必须要用一把锁对象，这个对象是num*/ 
			synchronized(num){
				if(!num.flag){
					try {
						 //操作wait()函数的必须和锁是同一个
						num.wait();
					} catch (Exception e) {
						e.printStackTrace();
					}
				} else {
					System.err.println(Thread.currentThread().getName() + "-> 偶数 ："+num.i);
					num.i++;
					num.flag = false;
					num.notify();
				}
				
			}
		}
	}

}
